/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gui;

import br.com.controllers.Controller;
import br.com.models.ClassRoom;
import br.com.models.Discipline;
import br.com.models.Teacher;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Valdigleis
 */
public class RemoveClassRoom extends javax.swing.JDialog {

    private JFrame parent;
    
    private ArrayList<Teacher> teachers;
    
    private ArrayList<Discipline> disciplines;
    
    private ArrayList<ClassRoom> classRooms;
    
    private Controller controller;
    
    private DefaultListModel model;
    
    /**
     * Creates new form RemoveClassRoom
     */
    public RemoveClassRoom(JFrame parent, ArrayList<Teacher> teachers, ArrayList<Discipline> disciplines) {
        super(parent, true);
        this.parent = parent;
        this.teachers = teachers;
        this.disciplines = disciplines;
        this.classRooms = new ArrayList<ClassRoom>();
        this.controller = new Controller();
        this.model = new DefaultListModel();
        initComponents();
        for(int i = 0; i < teachers.size(); i++){
            teacher.addItem(this.teachers.get(i).getName());
        }
        for(int i = 0; i < disciplines.size(); i++){
            discipline.addItem(this.disciplines.get(i).getName());
        }
        if(teachers.size() > 0 && disciplines.size() > 0){
            this.teacher.setSelectedIndex(0);
            this.discipline.setSelectedIndex(0);
        }
        this.tabel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        this.opt1.setSelected(false);
        this.opt2.setSelected(false);
        setResizable(false);
        Rectangle parentBounds = parent.getBounds();
        Dimension size = getSize();
        // Center in the parent
        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        teacher = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        discipline = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        opt1 = new javax.swing.JCheckBox();
        opt2 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabel = new javax.swing.JTable();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Parametros de Busca");

        jLabel2.setText("Professores");

        jLabel3.setText("Disciplinas");

        jLabel4.setText("Atributos da Busca");

        opt1.setText("Usar o professor na busca");

        opt2.setText("Usar a disciplina na busca");

        jButton1.setText("Realizar Busca");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Retornar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel5.setText("Turmas Encontradas");

        tabel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Turma", "Disciplina", "Professor", "Semestre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tabel);
        tabel.getColumnModel().getColumn(3).setResizable(false);

        jButton5.setText("Deletar Turma");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(teacher, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(discipline, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(opt1)
                                .addComponent(opt2)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 145, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(157, 157, 157))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton5)
                                .addGap(159, 159, 159))))))
            .addGroup(layout.createSequentialGroup()
                .addGap(279, 279, 279)
                .addComponent(jButton2)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(teacher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(discipline, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jLabel4)
                        .addGap(7, 7, 7)
                        .addComponent(opt1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(opt2))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton5))
                .addGap(42, 42, 42)
                .addComponent(jButton2)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        updateTabel();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        int itens[] = new int[this.tabel.getSelectedRows().length];
        itens = this.tabel.getSelectedRows();
        for(int i = 0; i < itens.length; i++){
            this.controller.remove(this.classRooms.get(itens[i]));
            if(i == itens.length - 1){
                JOptionPane.showMessageDialog(parent, "Turma(s) removida(s) com sucesso da Base!");
            }
        }
        updateTabel();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void updateTabel(){
        this.tabel.clearSelection();
        for(int i = 0; i < this.classRooms.size(); i++){
                        this.tabel.setValueAt("", i, 0);
                        this.tabel.setValueAt("", i, 1);
                        this.tabel.setValueAt("", i, 2);
                        this.tabel.setValueAt("", i, 3);
        }
        if(this.opt1.isSelected() && this.opt2.isSelected()){
            int i = this.teacher.getSelectedIndex();
            int j = this.discipline.getSelectedIndex();
            this.classRooms = this.controller.seacherByTeacherDiscipline(this.teachers.get(i), this.disciplines.get(j));
            for(int k = 0; k < this.classRooms.size(); k++){
                this.tabel.setValueAt(this.classRooms.get(k).getNumber(), k, 0);
                this.tabel.setValueAt(this.classRooms.get(k).getDiscipline().getName(), k, 1);
                this.tabel.setValueAt(this.classRooms.get(k).getTeacher().getName(), k, 2);
                this.tabel.setValueAt(this.classRooms.get(k).getSemester(), k, 3);
            }
        }else{
            if(this.opt1.isSelected()){
                int i = this.teacher.getSelectedIndex();
                this.classRooms = this.controller.seacherByTeacher(this.teachers.get(i));
                for(int k = 0; k < this.classRooms.size(); k++){
                    this.tabel.setValueAt(this.classRooms.get(k).getNumber(), k, 0);
                    this.tabel.setValueAt(this.classRooms.get(k).getDiscipline().getName(), k, 1);
                    this.tabel.setValueAt(this.classRooms.get(k).getTeacher().getName(), k, 2);
                    this.tabel.setValueAt(this.classRooms.get(k).getSemester(), k, 3);
                }
            }else{
                if(this.opt2.isSelected()){
                    int i = this.discipline.getSelectedIndex();
                    this.classRooms = this.controller.seacherByDiscipline(this.disciplines.get(i));
                    for(int k = 0; k < this.classRooms.size(); k++){
                        this.tabel.setValueAt(this.classRooms.get(k).getNumber(), k, 0);
                        this.tabel.setValueAt(this.classRooms.get(k).getDiscipline().getName(), k, 1);
                        this.tabel.setValueAt(this.classRooms.get(k).getTeacher().getName(), k, 2);
                        this.tabel.setValueAt(this.classRooms.get(k).getSemester(), k, 3);
                    }
                }
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox discipline;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JCheckBox opt1;
    private javax.swing.JCheckBox opt2;
    private javax.swing.JTable tabel;
    private javax.swing.JComboBox teacher;
    // End of variables declaration//GEN-END:variables
}
